package com.aizhixin.lab.company.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.company.domain.SignData;
import com.aizhixin.lab.course.course.repository.StuSignInRepository;
import com.aizhixin.lab.company.service.LogService;
import com.aizhixin.lab.course.course.service.ReleaseSignInService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;


@ServerEndpoint("/api/v1/base/socket/{accessToken}")
@Component
@Slf4j
public class WebSocketServer {
    private AccountDTO accountDTO = null;
    private static LogService logService;
    private static ReleaseSignInService signInService;
    private static StuSignInRepository stuSignInRepository;
    private static AccountService accountService;
    private static ConcurrentHashMap<String, WebSocketServer> webSocketSet = new ConcurrentHashMap<String, WebSocketServer>();
    private Session session;
    private static String token;
    private static Long logId;

    public Session getSession() {
        return session;
    }

    public AccountDTO getAccountDTO() {
        return accountDTO;
    }

    public static ConcurrentHashMap<String, WebSocketServer> getWebSocketSet() {
        return webSocketSet;
    }

    @Autowired
    public void setChatService(AccountService accountService) {
        WebSocketServer.accountService = accountService;
    }

    @Autowired
    public void setLogService(LogService logService) {
        WebSocketServer.logService = logService;
    }

    @Autowired
    public void setReleaseSigInRepository(ReleaseSignInService signInService) {
        WebSocketServer.signInService = signInService;
    }
//    @Autowired
//    public void setStuSignInRepository(StuSignInRepository stuSignInRepository) {
//        WebSocketServer.stuSignInRepository = stuSignInRepository;
//    }

    @OnOpen
    public void onOpen(Session session, @PathParam("accessToken") String accessToken) {
        AccountDTO account = accountService.getUserByToken(accessToken);
        if (account != null) {
            this.accountDTO = account;
            this.session = session;
            this.token = accessToken;
            webSocketSet.put(accessToken, this);
//            accountService.saveOnToken(account.getId(), accessToken);
            this.logId = logService.save(account.getId());
            System.out.println("用户:" + account.getName() + "已登录");
        }


    }

    @OnClose
    public void onClose() {
        AccountDTO account = accountService.getUserByToken(token);
        if (account != null) {
            if (this.logId != null)
                logService.update(this.logId);
//        if (accountDTO!= null)
//            accountService.clearOnToken(accountDTO.getId());
//            try {
//                webSocketSet.remove(token);
//
//            } catch (NullPointerException e) {
//
//            }
//        System.out.println("用户:" + accountDTO.getName()+":"+token + "已退出");
        }
    }
    @OnMessage
    public void sendMessage(String message) {
//        System.out.println(message);
//        session.getAsyncRemote().sendText("你是谁");
        if (message.equals("666")) {
            if (session!=null) {
                session.getAsyncRemote().sendText("保持连接中");
                return;
            }
        }
//        for (String s:message.split(",")){
//            SignData signData = signInService.releaseSign(message,null);
//            List<String> ids = signData.getIds();
//            for (String id:ids){
//                WebSocketServer webSocketServer = webSocketSet.get(id);
//                if (webSocketServer != null) {
////                    webSocketServer.sendMessage("你是谁");
//                    webSocketServer.session.getAsyncRemote().sendText(this.accountDTO.getName()+"老师提醒你签到了,"+signData.getSignInCode());
//
//                }
//            }
//        }
//        session.getAsyncRemote().sendText("发布签到成功");
    }

}
